%% 创建仿真场景和测试用例
function create_simulation_scenarios()
    % 初始化参数
    SVG_Star_10kV_System_Init;
    
    % 创建仿真场景模型
    model_name = 'Simulation_Scenarios';
    if bdIsLoaded(model_name)
        close_system(model_name, 0);
    end
    new_system(model_name);
    open_system(model_name);
    
    %% 1. 场景1：正常运行（平衡负载）
    create_scenario_normal(model_name);
    
    %% 2. 场景2：构网模式下电网电压跌落20%
    create_scenario_grid_sag_forming(model_name);
    
    %% 3. 场景3：跟网模式下电网电压跌落20%
    create_scenario_grid_sag_following(model_name);
    
    %% 4. 场景4：构网模式下电网电压骤升20%
    create_scenario_grid_swell_forming(model_name);
    
    %% 5. 场景5：跟网模式下电网电压骤升20%
    create_scenario_grid_swell_following(model_name);
    
    %% 6. 场景6：构网模式下A相电压跌落30%
    create_scenario_phase_a_sag(model_name);
    
    %% 7. 场景7：跟网模式下A相电压骤升30%
    create_scenario_phase_a_swell(model_name);
    
    %% 8. 场景8：负载不平衡
    create_scenario_unbalanced_load(model_name);
    
    %% 9. 创建测试控制器
    create_test_controller(model_name);
    
    %% 10. 创建数据记录和分析模块
    create_data_recording(model_name);
    
    % 保存模型
    save_system(model_name);
    disp(['仿真场景模型 ' model_name ' 创建完成！']);
end

%% 子函数：场景1 - 正常运行
function create_scenario_normal(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Scenario_1_Normal']);
    set_param([model_name '/Scenario_1_Normal'], 'Position', [100 100 300 200]);
    
    subsys = [model_name '/Scenario_1_Normal'];
    
    % 时间控制
    add_block('built-in/Clock', [subsys '/Time']);
    
    % 模式切换（0.5s时切换）
    add_block('built-in/Step', [subsys '/Mode_Switch']);
    set_param([subsys '/Mode_Switch'], 'Time', '0.5');
    set_param([subsys '/Mode_Switch'], 'Before', '0');  % 跟网模式
    set_param([subsys '/Mode_Switch'], 'After', '1');   % 构网模式
    
    % 电网参数（正常）
    add_block('built-in/Constant', [subsys '/Grid_Voltage']);
    set_param([subsys '/Grid_Voltage'], 'Value', '1.0');  % 标幺值
    
    add_block('built-in/Constant', [subsys '/Grid_Frequency']);
    set_param([subsys '/Grid_Frequency'], 'Value', '50');  % Hz
    
    % 负载参数（平衡）
    add_block('built-in/Constant', [subsys '/Load_Balance']);
    set_param([subsys '/Load_Balance'], 'Value', '[1 1 1]');  % 三相平衡
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Control_Mode']);
    add_block('built-in/Outport', [subsys '/Grid_Status']);
    add_block('built-in/Outport', [subsys '/Load_Status']);
end

%% 子函数：场景2 - 构网模式电压跌落
function create_scenario_grid_sag_forming(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Scenario_2_Sag_Forming']);
    set_param([model_name '/Scenario_2_Sag_Forming'], 'Position', [100 250 300 350]);
    
    subsys = [model_name '/Scenario_2_Sag_Forming'];
    
    % 时间控制
    add_block('built-in/Clock', [subsys '/Time']);
    
    % 模式切换（0.5s切换到构网模式）
    add_block('built-in/Step', [subsys '/Mode_Switch']);
    set_param([subsys '/Mode_Switch'], 'Time', '0.5');
    set_param([subsys '/Mode_Switch'], 'Before', '0');
    set_param([subsys '/Mode_Switch'], 'After', '1');
    
    % 电压跌落（1s-2s跌落20%）
    add_block('built-in/Subsystem', [subsys '/Voltage_Sag']);
    sag_sub = [subsys '/Voltage_Sag'];
    
    add_block('built-in/Step', [sag_sub '/Sag_Start']);
    set_param([sag_sub '/Sag_Start'], 'Time', '1.0');
    set_param([sag_sub '/Sag_Start'], 'Before', '1.0');
    set_param([sag_sub '/Sag_Start'], 'After', '0.8');  % 跌落20%
    
    add_block('built-in/Step', [sag_sub '/Sag_End']);
    set_param([sag_sub '/Sag_End'], 'Time', '2.0');
    set_param([sag_sub '/Sag_End'], 'Before', '0');
    set_param([sag_sub '/Sag_End'], 'After', '0.2');
    
    add_block('built-in/Sum', [sag_sub '/Total_Voltage']);
    
    % 频率扰动（模拟弱电网）
    add_block('built-in/Subsystem', [subsys '/Frequency_Disturbance']);
    freq_sub = [subsys '/Frequency_Disturbance'];
    
    add_block('built-in/Step', [freq_sub '/Freq_Drop']);
    set_param([freq_sub '/Freq_Drop'], 'Time', '1.0');
    set_param([freq_sub '/Freq_Drop'], 'Before', '50');
    set_param([freq_sub '/Freq_Drop'], 'After', '49.8');  % 跌落0.2Hz
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Control_Mode']);
    add_block('built-in/Outport', [subsys '/Grid_Voltage']);
    add_block('built-in/Outport', [subsys '/Grid_Frequency']);
end

%% 子函数：场景3 - 跟网模式电压跌落
function create_scenario_grid_sag_following(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Scenario_3_Sag_Following']);
    set_param([model_name '/Scenario_3_Sag_Following'], 'Position', [100 400 300 500]);
    
    subsys = [model_name '/Scenario_3_Sag_Following'];
    
    % 与场景2类似，但保持跟网模式
    add_block('built-in/Constant', [subsys '/Mode_Following']);
    set_param([subsys '/Mode_Following'], 'Value', '0');  % 始终跟网
    
    % 复制电压跌落逻辑
    add_block('built-in/Subsystem', [subsys '/Voltage_Sag']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Control_Mode']);
    add_block('built-in/Outport', [subsys '/Grid_Voltage']);
    add_block('built-in/Outport', [subsys '/Grid_Frequency']);
end

%% 子函数：场景4 - 构网模式电压骤升
function create_scenario_grid_swell_forming(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Scenario_4_Swell_Forming']);
    set_param([model_name '/Scenario_4_Swell_Forming'], 'Position', [100 550 300 650]);
    
    subsys = [model_name '/Scenario_4_Swell_Forming'];
    
    % 电压骤升（1s-2s骤升20%）
    add_block('built-in/Subsystem', [subsys '/Voltage_Swell']);
    swell_sub = [subsys '/Voltage_Swell'];
    
    add_block('built-in/Step', [swell_sub '/Swell_Start']);
    set_param([swell_sub '/Swell_Start'], 'Time', '1.0');
    set_param([swell_sub '/Swell_Start'], 'Before', '1.0');
    set_param([swell_sub '/Swell_Start'], 'After', '1.2');  % 骤升20%
    
    add_block('built-in/Step', [swell_sub '/Swell_End']);
    set_param([swell_sub '/Swell_End'], 'Time', '2.0');
    set_param([swell_sub '/Swell_End'], 'Before', '0');
    set_param([swell_sub '/Swell_End'], 'After', '-0.2');
    
    add_block('built-in/Sum', [swell_sub '/Total_Voltage']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Control_Mode']);
    add_block('built-in/Outport', [subsys '/Grid_Voltage']);
end

%% 子函数：场景6 - A相电压跌落
function create_scenario_phase_a_sag(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Scenario_6_PhaseA_Sag']);
    set_param([model_name '/Scenario_6_PhaseA_Sag'], 'Position', [400 100 600 200]);
    
    subsys = [model_name '/Scenario_6_PhaseA_Sag'];
    
    % 三相电压独立控制
    add_block('built-in/Subsystem', [subsys '/Three_Phase_Voltage']);
    tp_sub = [subsys '/Three_Phase_Voltage'];
    
    % A相跌落30%
    add_block('built-in/Step', [tp_sub '/Phase_A_Sag']);
    set_param([tp_sub '/Phase_A_Sag'], 'Time', '1.0');
    set_param([tp_sub '/Phase_A_Sag'], 'Before', '1.0');
    set_param([tp_sub '/Phase_A_Sag'], 'After', '0.7');
    
    % B相和C相保持正常
    add_block('built-in/Constant', [tp_sub '/Phase_B_Normal']);
    set_param([tp_sub '/Phase_B_Normal'], 'Value', '1.0');
    
    add_block('built-in/Constant', [tp_sub '/Phase_C_Normal']);
    set_param([tp_sub '/Phase_C_Normal'], 'Value', '1.0');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Va']);
    add_block('built-in/Outport', [subsys '/Vb']);
    add_block('built-in/Outport', [subsys '/Vc']);
end

%% 子函数：场景8 - 负载不平衡
function create_scenario_unbalanced_load(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Scenario_8_Unbalanced']);
    set_param([model_name '/Scenario_8_Unbalanced'], 'Position', [400 400 600 500]);
    
    subsys = [model_name '/Scenario_8_Unbalanced'];
    
    % A相：工业负载 2.1MW + 感性 1.15Mvar
    add_block('built-in/Constant', [subsys '/Phase_A_Load']);
    set_param([subsys '/Phase_A_Load'], 'Value', '[2.1e6 1.15e6]');
    
    % B相：商业负载 0.9MW + 容性 0.05Mvar
    add_block('built-in/Constant', [subsys '/Phase_B_Load']);
    set_param([subsys '/Phase_B_Load'], 'Value', '[0.9e6 -0.05e6]');
    
    % C相：居民负载 0.5MW + 容性 0.17Mvar
    add_block('built-in/Constant', [subsys '/Phase_C_Load']);
    set_param([subsys '/Phase_C_Load'], 'Value', '[0.5e6 -0.17e6]']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Load_A']);
    add_block('built-in/Outport', [subsys '/Load_B']);
    add_block('built-in/Outport', [subsys '/Load_C']);
end

%% 子函数：创建测试控制器
function create_test_controller(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Test_Controller']);
    set_param([model_name '/Test_Controller'], 'Position', [700 100 900 300]);
    
    subsys = [model_name '/Test_Controller'];
    
    % 场景选择器
    add_block('built-in/Constant', [subsys '/Scenario_Select']);
    set_param([subsys '/Scenario_Select'], 'Value', '1');  % 默认场景1
    
    % 时序控制
    add_block('built-in/Clock', [subsys '/Simulation_Time']);
    
    % 测试序列生成器
    add_block('built-in/Subsystem', [subsys '/Test_Sequencer']);
    seq_sub = [subsys '/Test_Sequencer'];
    
    % 时间节点定义
    add_block('built-in/Constant', [seq_sub '/t_mode_switch']);
    set_param([seq_sub '/t_mode_switch'], 'Value', '0.5');
    
    add_block('built-in/Constant', [seq_sub '/t_disturbance_start']);
    set_param([seq_sub '/t_disturbance_start'], 'Value', '1.0');
    
    add_block('built-in/Constant', [seq_sub '/t_disturbance_end']);
    set_param([seq_sub '/t_disturbance_end'], 'Value', '2.0');
    
    % 自动测试运行器
    add_block('built-in/Subsystem', [subsys '/Auto_Test_Runner']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Current_Scenario']);
    add_block('built-in/Outport', [subsys '/Test_Status']);
end

%% 子函数：创建数据记录和分析
function create_data_recording(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Data_Recording']);
    set_param([model_name '/Data_Recording'], 'Position', [700 400 900 600]);
    
    subsys = [model_name '/Data_Recording'];
    
    % 输入端口（所有需要记录的信号）
    signals = {'Grid_Voltage', 'Grid_Current', 'SVG_Voltage', 'SVG_Current', ...
               'P_Grid', 'Q_Grid', 'P_SVG', 'Q_SVG', 'DC_Voltage', 'SOC', ...
               'THD', 'Frequency', 'SCR'};
    
    for i = 1:length(signals)
        add_block('built-in/Inport', [subsys '/' signals{i}]);
    end
    
    % 数据记录模块
    add_block('built-in/To Workspace', [subsys '/Data_Logger']);
    set_param([subsys '/Data_Logger'], 'VariableName', 'SimData');
    set_param([subsys '/Data_Logger'], 'SaveFormat', 'Structure');
    
    % 实时显示
    add_block('built-in/Scope', [subsys '/Real_Time_Display']);
    set_param([subsys '/Real_Time_Display'], 'NumInputPorts', '6');
    
    % 性能指标计算
    add_block('built-in/Subsystem', [subsys '/Performance_Metrics']);
    perf_sub = [subsys '/Performance_Metrics'];
    
    % THD计算
    add_block('sps_lib/Sensors and Measurements/THD', [perf_sub '/THD_Analyzer']);
    
    % 响应时间计算
    add_block('built-in/Subsystem', [perf_sub '/Response_Time']);
    
    % 稳态误差计算
    add_block('built-in/Subsystem', [perf_sub '/Steady_State_Error']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Test_Results']);
end

%% 子函数：场景5 - 跟网模式电压骤升
function create_scenario_grid_swell_following(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Scenario_5_Swell_Following']);
    set_param([model_name '/Scenario_5_Swell_Following'], 'Position', [100 700 300 800]);
    
    subsys = [model_name '/Scenario_5_Swell_Following'];
    
    % 保持跟网模式
    add_block('built-in/Constant', [subsys '/Mode_Following']);
    set_param([subsys '/Mode_Following'], 'Value', '0');
    
    % 复制电压骤升逻辑
    add_block('built-in/Subsystem', [subsys '/Voltage_Swell']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Control_Mode']);
    add_block('built-in/Outport', [subsys '/Grid_Voltage']);
end

%% 子函数：场景7 - A相电压骤升
function create_scenario_phase_a_swell(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Scenario_7_PhaseA_Swell']);
    set_param([model_name '/Scenario_7_PhaseA_Swell'], 'Position', [400 250 600 350]);
    
    subsys = [model_name '/Scenario_7_PhaseA_Swell'];
    
    % A相骤升30%
    add_block('built-in/Step', [subsys '/Phase_A_Swell']);
    set_param([subsys '/Phase_A_Swell'], 'Time', '1.0');
    set_param([subsys '/Phase_A_Swell'], 'Before', '1.0');
    set_param([subsys '/Phase_A_Swell'], 'After', '1.3');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Va']);
    add_block('built-in/Outport', [subsys '/Vb']);
    add_block('built-in/Outport', [subsys '/Vc']);
end